#include "QQChatMessageModel.h"

QQChatMessageModel::QQChatMessageModel(QObject* parent)
	: QAbstractListModel(parent)
{
	mMessages.setCapacity(150);
}

QQChatMessageModel::~QQChatMessageModel()
{
	for (int i = 0; i < mMessages.size(); i++)
	{
		delete mMessages[i];
	}
}

void QQChatMessageModel::addMessage(QQChatMessage* message)
{
	this->beginInsertRows(QModelIndex(), mMessages.size(), mMessages.size());
	mMessages.insert(message->id % mMessages.size(), message);
	this->endInsertRows();
}

void QQChatMessageModel::addMessages(QList<QQChatMessage*> messages)
{
	this->beginInsertRows(QModelIndex(), mMessages.size(), mMessages.size() + messages.size() - 1);
	for (int i = 0; i < messages.size(); i++)
	{
		mMessages.insert(messages[i]->id % mMessages.size(), messages[i]);
	}
	this->endInsertRows();
}

void QQChatMessageModel::addTextMessage(long id, QQMessageSenderType senderType, QQMessageSendType sendType, const QString& name, const QString& avatar, const QString& text)
{
	addBaseMessage(id, senderType, sendType, name, avatar, text, QQMessageType::Text);
}

void QQChatMessageModel::addImageMessage(long id, QQMessageSenderType senderType, QQMessageSendType sendType, const QString& name, const QString& avatar, const QString& image)
{
	addBaseMessage(id, senderType, sendType, name, avatar, image, QQMessageType::Image);
}

void QQChatMessageModel::addFileMessage(long id, QQMessageSenderType senderType, QQMessageSendType sendType, const QString& name, const QString& avatar, const QString& file)
{
	addBaseMessage(id, senderType, sendType, name, avatar, file, QQMessageType::File);
}

void QQChatMessageModel::addVoiceMessage(long id, QQMessageSenderType senderType, QQMessageSendType sendType, const QString& name, const QString& avatar, const QString& voice)
{
	addBaseMessage(id, senderType, sendType, name, avatar, voice, QQMessageType::Voice);
}

void QQChatMessageModel::addVideoMessage(long id, QQMessageSenderType senderType, QQMessageSendType sendType, const QString& name, const QString& avatar, const QString& video)
{
	addBaseMessage(id, senderType, sendType, name, avatar, video, QQMessageType::Video);
}

void QQChatMessageModel::addUrlMessage(long id, QQMessageSenderType senderType, QQMessageSendType sendType, const QString& name, const QString& avatar, const QString& url)
{
	addBaseMessage(id, senderType, sendType, name, avatar, url, QQMessageType::Url);
}

void QQChatMessageModel::addMixMessage(long id, QQMessageSenderType senderType, QQMessageSendType sendType, const QString& name, const QString& avatar, const QString& mix)
{
	//to do	
	addBaseMessage(id, senderType, sendType, name, avatar, mix, QQMessageType::Mix);
}

QQChatMessage* QQChatMessageModel::getMessage(long id)
{
	for (int i = 0; i < mMessages.size(); i++)
	{
		if (mMessages[i]->id == id)
		{
			return mMessages[i];
		}
	}
	return nullptr;
}

void QQChatMessageModel::clearMessage()
{
	mMessages.clear();
}

int QQChatMessageModel::rowCount(const QModelIndex& parent) const
{
	Q_UNUSED(parent);
	return mMessages.size();
}

QVariant QQChatMessageModel::data(const QModelIndex& index, int role) const
{
	if (role == Qt::UserRole)
	{
		return QVariant::fromValue(mMessages[index.row()]);
	}
	return QVariant();
}

void QQChatMessageModel::addBaseMessage(long id, QQMessageSenderType senderType, QQMessageSendType sendType, const QString& name, const QString& avatar, const QString& content, QQMessageType type)
{
	this->beginInsertRows(QModelIndex(), mMessages.size(), mMessages.size());
	QQChatMessage* message = new QQChatMessage;
	message->id = id;
	message->name = name;
	message->avatar = avatar;
	message->content = content;
	message->type = type;
	message->senderType = senderType;
	message->sendType = sendType;
	mMessages.insert(id % mMessages.size(), message);
	this->endInsertRows();
}
